package com.itheima.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.system.mapper.RoleMapper;
import com.itheima.system.pojo.Permission;
import com.itheima.system.pojo.Role;
import com.itheima.system.pojo.vo.AssignPremVo;
import com.itheima.system.service.RoleService;
import com.itheima.common.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author IntelliJ IDEA
 * @date 2020/10/26  9:24
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 分配权限
     * id：角色id
     * permIs： 权限id的集合
     */
    @Override
    public void assignPrem(AssignPremVo assignPremVo) {
        //根据roleId批量删除中间表
        roleMapper.deleteRoleAndPermissionByRoleId(assignPremVo.getId());
        Set<Permission> result=new HashSet<Permission>(0);
        //根据roleIds批量查询Permission
        Set<Permission> set=roleMapper.findBatchPermissionByPermIds(assignPremVo.getPermIds());
        result.addAll(set);
        //根据权限集合查询权限api的集合（根据type和pid的集合查询,子集权限）
        Set<Permission> permissionSet=roleMapper.findBatchPermissionApiBySet(set.stream().map(Permission::getPid).collect(Collectors.toSet()));
        result.addAll(permissionSet);
        //新增中间表
        roleMapper.setRoleAndPermission(assignPremVo.getId(),result.stream().map(Permission::getId).collect(Collectors.toSet()));
    }

    /**
     * 添加角色
     */
    @Override
    public Integer addRole(Role role){
        role.setId(String.valueOf(idWorker.nextId()));
        return roleMapper.addRole(role);
    }

    /**
     * 更新角色
     */
    @Override
    public Integer updateRoleByRoleId(Role role) {
        return roleMapper.updateRoleByRoleId(role);
    }

    /**
     *  根据id删除角色
     */
    @Override
    public Integer deleteRoleById(String id) {
        return roleMapper.deleteRoleById(id);
    }

    /**
     * 根据id查询角色信息
     */
    @Override
    public Role findRoleById(String id) {
        return roleMapper.findRoleById(id);
    }

    /**
     * 分页查询角色列表
     */
    @Override
    public PageResult<Role> findAllRoleByPage(String companyId, Integer page, Integer size) {
        //设置分页参数信息
        PageHelper.startPage(page,size);
        //查询
        List<Role> roleList = roleMapper.findAllRole(companyId);
        //封装PageInfo对象
        PageInfo<Role> info=new PageInfo<Role>(roleList);
        //返回
        return new PageResult<Role>(info.getTotal(),info.getList());
    }

    /**
     *  根据companyId查询改企业下的所有角色
     */
    @Override
    public List<Role> findAllRole(String companyId) {
        return roleMapper.findAllRole(companyId);
    }

    /**
     * 根据用户id查询角色结合
     */
    @Override
    public Set<String> findRoleIdsByUserId(String userId) {
        return roleMapper.findRoleIdsByUserId(userId);
    }
}
